#include "login.h"
#include "ticketing.h"
#include "myfont.h"
#include "admin.h"


bool read_user_file(const char *path)
{
    FILE *fp = fopen(path, "r");
    if (fp == NULL) {
        printf("无法打开文件 %s\n", path);
        return false;
    }
    memset(user_list, 0, sizeof(user_list));

    char line[256];
    bool fristline = true;
    
    while(fgets(line, sizeof(line), fp) != NULL) 
    {
        line[strcspn(line, "\n")] = '\0';
        if(!fristline)
        {
            strcat(user_list, "\n"); // 添加选项分隔符
        }
        else
        {
            fristline = false;
        }

        strcat(user_list, line); // 将读取的行添加到文件内容字符串中
    }

    fclose(fp);
    return true;
}

void exit_admin_btn_event_cb(lv_event_t * e)
{
    lv_obj_clean(my_admin);
    login_init();
    lv_scr_load(login);
    lv_obj_del(my_admin);
    my_admin=NULL;
}

void user_dropdown_event_cb(lv_event_t * e)
{
    lv_obj_t * dropdown = lv_event_get_target(e);

    // char buf[128];
    // lv_dropdown_get_selected_str(dropdown, buf, sizeof(buf));

    // // 获取关联的结果标签
    // lv_obj_t * result_label = lv_obj_get_user_data(dropdown);

    // // 更新结果标签
    // char msg[150];
    // snprintf(msg, sizeof(msg), "已选择: %s", buf);
    // lv_label_set_text(result_label, msg);
}


// 重置座位状态按钮事件处理
void reset_seats_event_cb(lv_event_t * e) 
{
    // 删除/session目录及其所有内容
    DIR *dir = opendir("/session");
    if (dir) {
        struct dirent *entry;
        while ((entry = readdir(dir)) != NULL) {
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            
            char filepath[256];
            snprintf(filepath, sizeof(filepath), "/session/%s", entry->d_name);
            remove(filepath);
        }
        closedir(dir);
        rmdir("/session");
    }
    
    // 重新创建/session目录
    mkdir("/session", 0777);
    
    // 显示成功提示
    lv_obj_t *success_popup = lv_msgbox_create(my_admin, "操作成功", "座位状态已重置!", NULL, true);
    lv_obj_set_style_text_font(success_popup, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_align(success_popup, LV_ALIGN_CENTER, 0, 0);
}


void admin_init()
{
    my_admin=lv_obj_create(NULL);
    lv_scr_load(my_admin);
    lv_obj_del(login);
    login=NULL;

    lv_obj_t * admin_title = lv_label_create(my_admin);//取票标题
    lv_label_set_text(admin_title, "管理员界面");
    lv_obj_align(admin_title,LV_ALIGN_TOP_MID, 0,0);
    lv_obj_set_style_text_font(admin_title, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_bg_color(admin_title,lv_color_hex(0x0000ff),0);

    lv_obj_set_size(my_admin, 800, 480);
    lv_obj_set_style_bg_color(my_admin, lv_color_hex(0xffffff), 0);
    lv_obj_set_style_border_color(my_admin, lv_color_hex(0x0000ff), 0);
    lv_obj_set_style_border_width(my_admin, 2, 0);
    lv_obj_set_scroll_dir(my_admin, LV_DIR_NONE);
    lv_obj_center(my_admin);

    jpg_bg = lv_img_create(my_admin);
    lv_img_set_src(jpg_bg, "S:/imgs/353.jpg");
    lv_obj_align(jpg_bg, LV_ALIGN_CENTER, 0, 0);
    lv_obj_move_background(jpg_bg);

    //设置退出按钮
    exit_btn = lv_btn_create(my_admin);
    lv_obj_set_size(exit_btn, 40, 40);
    lv_style_init(&exit_btn_style);
    lv_style_set_radius(&exit_btn_style, 0);
    lv_style_set_bg_color(&exit_btn_style, lv_color_hex(0x2B2B2B));
    lv_style_set_bg_opa(&exit_btn_style, LV_OPA_100);
    lv_obj_add_style(exit_btn, &exit_btn_style, 0);
    lv_obj_add_event_cb(exit_btn, exit_admin_btn_event_cb, LV_EVENT_CLICKED, NULL);
    //设置退出箭头label
    lv_obj_t *exit_label = lv_label_create(exit_btn);
    lv_label_set_text(exit_label, LV_SYMBOL_LEFT);
    lv_obj_center(exit_label);

    //设置用户下拉框
    user_dropdown = lv_dropdown_create(my_admin);
    lv_obj_set_size(user_dropdown, 300, 50);
    lv_obj_align(user_dropdown, LV_ALIGN_CENTER, 0, -50);
    lv_obj_set_style_text_font(user_dropdown, &lv_font_montserrat_20, 0);

    
    if(!read_user_file("appuser/user.txt"))
    {
        // 文件读取失败，创建错误标签
        lv_obj_t * error_label = lv_label_create(my_admin);
        lv_label_set_text(error_label, "无法加载用户列表");
        lv_obj_align(error_label, LV_ALIGN_CENTER, 0, 0);
        lv_obj_set_style_text_font(error_label, &lv_font_source_han_sans_bold_20, 0);
        return;
    }
    lv_dropdown_set_options(user_dropdown, user_list); // 设置下拉列表的选项
    lv_obj_set_style_bg_color(user_dropdown, lv_color_hex(0xf0f0f0), 0);
    lv_obj_set_style_border_width(user_dropdown, 2, 0);
    lv_obj_set_style_border_color(user_dropdown, lv_color_hex(0x0075ff), 0);
    lv_obj_set_style_text_font(user_dropdown, &lv_font_montserrat_20, 0);
    lv_obj_t * list2 = lv_dropdown_get_list(user_dropdown);
    lv_obj_set_style_text_font(list2, &lv_font_montserrat_20, 0);
    lv_obj_add_event_cb(user_dropdown, user_dropdown_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    

    //设置电影场次结果标签
    // lv_obj_t * result_label2 = lv_label_create(my_admin);
    // lv_obj_set_style_text_font(result_label2, &lv_font_montserrat_20, 0);
    // lv_obj_align(result_label2, LV_ALIGN_CENTER, 0, 200);
    // lv_obj_set_user_data(user_dropdown, result_label2); // 关联结果标签

     // 创建重置座位状态按钮
    lv_obj_t *reset_seats_btn = lv_btn_create(my_admin);
    lv_obj_set_size(reset_seats_btn, 200, 50);
    lv_obj_align(reset_seats_btn, LV_ALIGN_CENTER, 0, 100);
    lv_obj_set_style_bg_color(reset_seats_btn, lv_color_hex(0xFF0000), 0); // 红色按钮
    
    // 设置重置按钮文本
    lv_obj_t *reset_label = lv_label_create(reset_seats_btn);
    lv_label_set_text(reset_label, "重置座位状态");
    lv_obj_center(reset_label);
    lv_obj_set_style_text_font(reset_label, &lv_font_source_han_sans_bold_20, 0);
    lv_obj_set_style_text_color(reset_label, lv_color_white(), 0);
    
    // 添加重置按钮事件
    lv_obj_add_event_cb(reset_seats_btn, reset_seats_event_cb, LV_EVENT_CLICKED, NULL);

}